package org.aeon.dao;

import org.aeon.entity.UserBorrowBook

/**
 * @author: MissingAeon
 * @date: 2024/1/19 9:40
 * @description:
 */
class UserBorrowBookDao : BasicDao<String, UserBorrowBook> {
    private companion object {
        val ubb = hashMapOf<String, UserBorrowBook>()

        init {
        }
    }

    override fun insert(t: UserBorrowBook): Boolean {
        if (!ubb.containsKey(t.id)) {
            ubb += t.id to t.copy()
            return true
        }
        return false
    }

    override fun modify(t: UserBorrowBook): Boolean {
        if (ubb.containsKey(t.id)) {
            ubb += t.id to t.copy()
            return true
        }
        return false
    }

    override fun remove(t: UserBorrowBook): Boolean {
        if (ubb.containsKey(t.id)) {
            ubb.remove(t.id)
            return true
        }
        return false
    }

    override fun query(func: (UserBorrowBook) -> Boolean): List<UserBorrowBook> {
        return ubb.asSequence()
            .filter { func(it.value) }
            .map { it.value.copy() }
            .toList()
    }

    override fun queryById(k: String): UserBorrowBook? = ubb[k]

    // 这里只会看该用户名有没有借书记录
    override fun exist(k: String): Boolean = ubb.containsKey(k)

    override fun count(): Int = ubb.size
}
